﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace ContosoDashboard
{
    public class NotificationService
    {
        private static readonly NotificationService _instance;

        static NotificationService()
        {
            _instance = new NotificationService();
        }

        private readonly NotificationWindow _nw;
        private readonly TextBlock _nwTextBlock;

        public NotificationService()
        {
            _nw = new NotificationWindow();
            _nwTextBlock = new TextBlock
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center
            };
            _nw.Content = new Border
            {
                BorderThickness = new Thickness(2),
                BorderBrush = new SolidColorBrush { Color = (Color)Application.Current.Resources["AccentColor"] },
                Background = new SolidColorBrush { Color = Colors.White },
                VerticalAlignment = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Child = _nwTextBlock
            };

            MouseButtonEventHandler[] handler = {null};
            handler[0] = (s, a) =>
            {
                _nw.Close();
                _nw.Content.MouseLeftButtonDown -= handler[0];

                //if (!Application.Current.MainWindow.IsActive)
                //{
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    Application.Current.MainWindow.Activate();
                //}
            };

            _nw.Content.MouseLeftButtonDown += handler[0];
        }

        public static NotificationService Current
        {
            get
            {
                return _instance;
            }
        }

        public void ShowNotification(string text)
        {
            if (_nw.Visibility == Visibility.Visible)
                _nw.Close();
            _nwTextBlock.Text = text;
            _nw.Show(3000);

        }
    }
}
